const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.Label)
    tipLabel: cc.Label = null;

    stateStr = '';
    progress = 0;
    splash = null;
    isLoading = false;

    onLoad() {
        
        if(!cc.sys.isNative && cc.sys.isMobile){
            let cvs = this.node.getComponent(cc.Canvas);
            cvs.fitWidth = true;
            cvs.fitHeight = true;
        }
        this.tipLabel.string = this.stateStr;
        this.startPreloading();
    }

    startPreloading(){
        this.stateStr = '正在加载资源，请稍后';
        this.isLoading = true;

        cc.loader.loadResDir('textures', 
            //加载过程中调用
            (complatedCount, totalCount, item) => {
                if(this.isLoading){
                    this.progress = complatedCount / totalCount;
                }
            },
            //全部加载完成或出错调用
            (err, resource, urls) => {
                if(err){
                    this.isLoading = false;
                    this.stateStr = '加载资源出错';
                    return;
                }
                this.onLoadComplate();
            });
    }

    onLoadComplate(){
        this.stateStr = '准备登陆';
        cc.director.loadScene('login');
    }

    update(dt){
        if(this.stateStr.length == 0){
            return;
        }
        this.tipLabel.string = this.stateStr + ' ';
        if(this.isLoading){
            this.tipLabel.string += Math.floor(this.progress * 100) + '%';
        }
        else{
            let t = Math.floor(Date.now() / 1000) % 4;
            for(let i = 0; i < t; i++){
                this.tipLabel.string += '.';
            }
        }

    }
}
